package com.aube.plugin.origin;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aube.plugin.wap.H5WrapperActivity;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.DLBridge;
import com.ryg.dynamicload.internal.DLIntent;

import java.util.List;

import static com.aube.plugin.origin.R.id.img_item;

public class HorizontalPagerAdapter extends PagerAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<DataConvertUtil.SimpleIndexHeader> mData;
    private Resources mResources;
    private DLBridge mBridge;

    public HorizontalPagerAdapter(final Context context, Resources resources, DLBridge listener, List<DataConvertUtil.SimpleIndexHeader> data) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mData = data;
        mResources = resources;
        mBridge = listener;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemPosition(final Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        final View view = inflate(R.layout.home_vp_item, container, false);
        ImageView mImg = (ImageView) view.findViewById(img_item);
        final DataConvertUtil.SimpleIndexHeader item = mData.get(position);
        ImageLoader.getInstance(mContext).fillImage(mImg, item.picurl);
        mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mBridge == null)
                    return;
                if(mData.get(position).isH5()) {
                    DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.wap.H5WrapperActivity");
                    intent.putExtra(H5WrapperActivity.WEB_POST, true);
                    intent.putExtra(H5WrapperActivity.WEB_TITLE, item.title);
                    intent.putExtra(H5WrapperActivity.WEB_URL, item.url);
                    mBridge.openPluginActivity(intent);
                } else {
                    mBridge.onClickToVideo(item.channel);
                }
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(final View view, final Object object) {
        return view.equals(object);
    }

    @Override
    public void destroyItem(final ViewGroup container, final int position, final Object object) {
        container.removeView((View) object);
    }

    public View inflate(int id, ViewGroup root, boolean attachToRoot) {

        final XmlResourceParser parser = mResources.getLayout(id);
        try {
            return mLayoutInflater.inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

}
